/*
 * EVENT
 */

function bind(type, handler) {
	$(window).bind('backframe_' + type, handler);
}

function trigger(type, data) {
	$(window).trigger('backframe_' + type, [data]);
}

/*
 * CONSOLE MENEGAMENT
 */

function action(name, handler) {
	trigger('addAction', {name: name, handler: handler});
}

function del_action(name) {
	trigger('delAction', {name: name});
}

function channel(name, location) {
	trigger('addChannel', {name: name, location: location});
}

function del_channel(name) {
	trigger('delChannel', {name: name});
}

function client(channel, name, data) {
	trigger('addClient', {channel: channel, name: name, data: data});
}

function del_client(channel, name) {
	trigger('delClient', {channel: channel, name: name});
}

/*
 * CONSOLE VISUAL MENAGEMENT
 */

function populate_actions() {
	trigger('populateActions');
}

function populate_channels() {
	trigger('populateChannels');
}

function populate_clients(channels) {
	if (channels != undefined)
		trigger('populateClients', {channels: channels});
	else
		trigger('populateClients', {channels: $('select[@name="channels"]').selectedOptions()});
}

/*
 * PRESENTATION
 */

function render(code, wrapped) {
	trigger('render', {code: code, wrapped: wrapped});
}

function show(container) {
	trigger('show', {container: container});
}

function hide(container) {
	trigger('hide', {container: container})
}

/*
 * GENERAL
 */

function load(location) {
	trigger('loadScript', {location: location});
}

function message(message, clients) {
	trigger('sendMessage', {message: message, clients: clients});
}

function message_selected_clients(message) {
	trigger('sendMessage', {message: message, clients: selected_clients_ex()});
}

function selected_channels() {
	return $('select[@name="channels"]').selectedOptionsByName();
}

function selected_clients() {
	return $('select[@name="clients"]').selectedOptionsByName();
}

function selected_clients_ex() {
	var clients = new Array();
	var options = $('select[@name="clients"]').selectedOptionsPairs();
	
	for (var index = 0; index < options.length; index++)
		clients.push({client: options[index].name, channel: options[index].value});
		
	return clients;
}